package com.cip.admin.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cip.admin.dao.SMSDAO;
import com.cip.admin.model.Sms;
import com.cip.admin.model.User;
import com.cip.admin.service.ImportFileService;
import com.cip.admin.service.SMSService;
import com.cip.common.util.ApiServiceUtil;
import com.cip.common.util.CommonUtil;
import com.cip.common.util.SecurityUtil;

public class SMSServiceImpl implements SMSService{


	private SMSDAO smsDAO;
	private ImportFileService importFileService;
	public static final Log logger = LogFactory.getLog(com.cip.admin.service.impl.SMSServiceImpl.class);
	
	/**
	 * Sent sms for single user
	 */
	public String sentSMS(Sms sms) throws Exception {
		List<Sms> smsList = new ArrayList<Sms>();
		
		String[] mobileNumbers = sms.getMobileNumber().split(",");
		
		for (String mobileNumber : mobileNumbers) {
			
			String responseText = ApiServiceUtil.apiServiceCallGet(CommonUtil.constructUrl("91"+mobileNumber, sms.getTextMessage()));
			
			boolean status =  (responseText.split("=")[1].indexOf(0)) == '1' ? true  :  false;
			
			logger.info("mobileNumber : "+mobileNumber + " Text Message :"+ sms.getTextMessage() +" sms sent Status : "+ status);
			
			smsList.add(setUpdateSMS(mobileNumber, sms.getTextMessage(), status));
		}
		
		smsDAO.bulkUpdateSMS(smsList);
		
		return "SUCCESS";
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String sentUploadSms(Map<String,String> smsParam,File fileUpload) throws Exception {
		List<Map<String, String>> csvValues = importFileService.getDataFromCSV(fileUpload);
		List<Sms> smsList = new ArrayList<Sms>();
		
		for (Map<String, String> map : csvValues) {
			
			String responseText = ApiServiceUtil.apiServiceCallGet(CommonUtil.constructUrl("91"+map.get("phoneno"), smsParam.get("messageText")));
			
			boolean status =  (responseText.split("=")[1].indexOf(0)) == '1' ? true  :  false;
			
			logger.info("mobileNumber : "+map.get("phoneno") + " Text Message :"+ smsParam.get("messageText") +" sms sent Status : "+ status);
			
			smsList.add(setUpdateSMS(map.get("phoneno"), smsParam.get("messageText"), status));
		}
		
		smsDAO.bulkUpdateSMS(smsList);
		
		return "SUCCESS";
	}
	
	/**
	 * <p>
	 * 
	 * </p>
	 * @return
	 * @throws Exception
	 */
	public String broadcastSms(String messageText) throws Exception {
		List<User> user = smsDAO.getAllUsers();
		
		smsDAO.bulkUpdateSMS(constructSmsList(user,messageText));
		
		return "SUCCESS";
	}

	/**
	 * <p>
	 * 
	 * </p>
	 * @return
	 * @throws Exception
	 */
	public String broadcastSmsSelect(Map<String,String> broadcastSmsSelect) throws Exception {
		
		List<User> user= smsDAO.broadcastSmsSelect(broadcastSmsSelect);
		
		smsDAO.bulkUpdateSMS(constructSmsList(user,broadcastSmsSelect.get("messageText")));
		
		return "SUCCESS";
	}
	
	/**
	 * 
	 */
	private List<Sms> constructSmsList(List<User> user,String messageText) throws Exception {
		List<Sms> smsList = new ArrayList<Sms>();
		
		for (User userList : user) {
			
			String responseText = ApiServiceUtil.apiServiceCallGet(CommonUtil.constructUrl("91"+userList.getPhoneNumber(), messageText));
			
			boolean status =  (responseText.split("=")[1].indexOf(0)) == '1' ? true  :  false;
			
			logger.info("mobileNumber : "+userList.getPhoneNumber()+ " Text Message :"+ messageText +" sms sent Status : "+ status);
			
			smsList.add(setUpdateSMS(userList.getPhoneNumber(),messageText,status));
		}
		
		return smsList;
	}
	
	/**
	 * 
	 */
	private Sms setUpdateSMS(String phoneNO, String messageText, boolean status) {
		Sms bulkSMS = new Sms();
		User user = SecurityUtil.getThreadLocalUser().get();
		
		bulkSMS.setTextMessage(messageText);
		bulkSMS.setTenantId(user.getTenantId());
		bulkSMS.setStatus(status);
		
		return bulkSMS;
	}
	
	public SMSDAO getSmsDAO() {
		return smsDAO;
	}

	public void setSmsDAO(SMSDAO smsDAO) {
		this.smsDAO = smsDAO;
	}

	public ImportFileService getImportFileService() {
		return importFileService;
	}

	public void setImportFileService(ImportFileService importFileService) {
		this.importFileService = importFileService;
	}

}
